package com.globalresources.commons;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class PublicUtil {
	

		public static String getStrNowDate() {
			java.util.Date tdate = new java.util.Date();
			String nowtime = new Timestamp(tdate.getTime()).toString();
			nowtime = nowtime.substring(0, 10);
			String nowYear = nowtime.substring(0, 4);
			String nowMonth = nowtime.substring(5, 7);
			String nowDay = nowtime.substring(8, 10);
			String nowdate = nowYear + nowMonth + nowDay;
			return nowdate;

		}

	
		public static String getNowDate() {
			java.util.Date tdate = new java.util.Date();
			String nowtime = new Timestamp(tdate.getTime()).toString();
			nowtime = nowtime.substring(0, 10);
			return nowtime;
		}
		
	   /**
	    * <p>获取date对应当天零时零分
	   *@author aaron.lu(luzhiyuan)
	   *@param date Date 时刻
	   *@return
	   *@throws ParseException
	    */
	   public static Date getFirstSecondInDay(Date date) throws ParseException{
			SimpleDateFormat sfyear = new SimpleDateFormat("yyyy-MM-dd");
			String time = sfyear.format(date);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	
			String str = time + " 00:00:00:000";
			Date result = null;
			result = sf.parse(str);
			return result;
		}
		
	   /**
	    * <p>获取date对应当天最后一秒
	   *@author aaron.lu(luzhiyuan)
	   *@param date Date 时刻
	   *@return
	   *@throws ParseException
	    */
       public static Date getLastSecondInDay(Date date) throws ParseException {
			SimpleDateFormat sfyear = new SimpleDateFormat("yyyy-MM-dd");
			String time = sfyear.format(date);
	
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	
			String str = time + " 23:59:59:000";
			Date result = null;
			result = sf.parse(str);
			return result;
		}
		
       /**
        * <p>判断date是否在今天
       *@author aaron.lu(luzhiyuan)
       *@param date Date 时刻
       *@return
       *@throws ParseException
        */
		public static boolean isToday(Date date) throws ParseException{
			Date _begin_slice = getFirstSecondInDay(new Date());
			Date _end_slice = getLastSecondInDay(new Date());
			boolean result = false;
			if (null != date ) {
				if (date.before(_end_slice) && date.after(_begin_slice)) {
					result = true;
				}
			}
			
			return result;
		}
		
		public static Long getDistance(Date date){
			Date current_date = new Date();
			Long _distance = current_date.getTime() - date.getTime();
			Long result = _distance / 3600000 ;
			return result;
		}
		
		public static void main(String[] args) throws ParseException {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
			String date = "2009-08-14 10:27:00:000";
			System.out.println(PublicUtil.getDistance(sf.parse(date)));
		}
}
